# -*- coding: utf-8 -*-

import requests
from flask import Blueprint, request, jsonify

from apps import exception

bp = Blueprint('login', __name__, url_prefix='/login')


@bp.route('', methods=['GET', 'POST'])
def login():
    content = request.json or {}
    code = content.get('code', None)

    if not code:
        raise exception.ParameterException('code', code)

    url = 'https://api.weixin.qq.com/sns/jscode2session?appid=wxa1a975e6308e8944&secret=cfd7da376f666fbfcc26f60fcc8944f9&js_code={}&grant_type=authorization_code'
    response = requests.get(url.format(code)).text
    if not response:
        raise exception.ParameterException('key', response)
    data = {
        'code': 200,
        'msg': 'login on'
    }
    return jsonify(data)
